﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS.Module
{
    public partial class ucDanhMuc : System.Web.UI.UserControl
    {
        private static DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["NhanHieu"] != null)
                {
                    loadDM2(10);
                    Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                    Session["NhanHieu"] = Request.QueryString["NhanHieu"].ToString();
                }

                else
                {
                    if (Request.QueryString["LoaiSP"] == null)
                    {
                        if (Request.QueryString["FilterDM"] != null)
                        {
                            loadDM2(10);
                            Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                        }
                        if (Request.QueryString["Filter"] != null)
                        {
                            //rpt.Visible = false;
                            rpt.Visible = true;
                            loadDM(10);
                            rpt2.Visible = false;
                            //imgbt.Visible = true;
                            //lbDM.Visible = true;
                            //cmsLoaiSanPhamDO obj = new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = int.Parse(Session["LoaiSP"].ToString()) });
                            //lbDM.Text = obj.TenLoaiSanPham;
                        }
                        else
                        {
                            loadDM(10);
                            Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                        }
                    }
                    
                    else if ((Request.QueryString["LoaiSP"] != null))
                    {
                        loadDM(10);
                        rpt.Visible = true;
                        rpt2.Visible = false;
                        //imgbt.Visible = true;
                        //lbDM.Visible = true;
                        //cmsLoaiSanPhamDO obj = new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = int.Parse(Request.QueryString["LoaiSP"].ToString()) });
                        //lbDM.Text = obj.TenLoaiSanPham;
                    }
                }
            }
        }

        public void loadDM(int so)
        {
            dt = new cmsLoaiSanPhamBL().SelectAll(so);
            rpt.DataSource = dt;
            rpt.DataBind();
            if ((Request.QueryString["Filter"] != null) || (Request.QueryString["LoaiSP"] != null))
            {
                int id = 0;
                if (Request.QueryString["Filter"] != null)
                {
                    if (Session["LoaiSP"] != null)
                        id = int.Parse(Session["LoaiSP"].ToString());
                }
                else
                    id = int.Parse(Request.QueryString["LoaiSP"].ToString());
                int i=0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (int.Parse(dr.ItemArray[0].ToString()) == id)
                        //((ImageButton)e.Item.FindControl("imgbt2")).Attributes.Add("Visible", "true");
                        ((ImageButton)rpt.Items[i].FindControl("imgbt2")).Visible = true;
                    i++;
                }
            }
        }

        public void loadDM2(int so)
        {
            lkXem2.Visible = true;
            lbXem.Visible = false;
            rpt2.Visible = true;
            rpt.Visible = false;
            rpt2.DataSource = new cmsLoaiSanPhamBL().SelectAll(so);
            rpt2.DataBind();
        }

        public void init(bool gt)
        {
            lbXem.Visible = gt;
            lkAn.Visible = !gt;
            lkAn2.Visible = !gt;
            lkXem2.Visible = !gt;
        }

        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        protected void imgbt_Click(object sender, ImageClickEventArgs e)
        {
            //loadDM();
            ////loadDM2();
            //if (Session["DuongDan"] != null)
            //    Response.Redirect("~" + Session["DuongDan"].ToString());
            Response.Redirect("/SanPham.aspx");
        }
        public string check(string id1)
        {
            string s="";
            if ((Request.QueryString["Filter"] != null) || (Request.QueryString["LoaiSP"] != null))
            {
                string id = "";
                if (Request.QueryString["Filter"] != null)
                    id = Request.QueryString["Filter"].ToString();
                else
                    id = Request.QueryString["LoaiSP"].ToString();
                if(id1 == id)
                    s = "<asp:ImageButton ID=\"imgbt2\" runat=\"server\" CssClass=\"btn\" ImageUrl=\"~/images/3.png\" Height=\"10px\" Width=\"10px\" onclick=\"imgbt_Click\" />";
            }
            return s;
        }
        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //{
            //    if ((Request.QueryString["Filter"] != null) || (Request.QueryString["LoaiSP"] != null))
            //    {
            //        string id = "";
            //        if (Request.QueryString["Filter"] != null)
            //            id = Request.QueryString["Filter"].ToString();
            //        else
            //            id = Request.QueryString["LoaiSP"].ToString();
            //        foreach (DataRow dr in dt.Rows)
            //        {
            //            if (int.Parse(dr.ItemArray[0].ToString()) == int.Parse(id))
            //                //((ImageButton)e.Item.FindControl("imgbt2")).Attributes.Add("Visible", "true");
            //                ((ImageButton)e.Item.FindControl("imgbt2")).Visible = true;
            //        }
            //        //ImageButton btimg = (ImageButton)e.Item.FindControl("imgbt2");
            //        //string id2 = btimg.CommandArgument.ToString();
            //        //if(id2 == id)
            //        //    ((ImageButton)e.Item.FindControl("imgbt2")).Visible = true;
            //    }
            //}
        }

        protected void btnXemThem_Click(object sender, EventArgs e)
        {
            loadDM(0);
            lkAn.Visible = true;
            lbXem.Visible = false;
            lkXem2.Visible = false;
            lkAn2.Visible = false;
        }

        protected void btnAn_Click(object sender, EventArgs e)
        {
            loadDM(10);
            lbXem.Visible = true;
            lkXem2.Visible = false;
            lkAn.Visible = false;
        }

        protected void btnXemThem2_Click(object sender, EventArgs e)
        {
            loadDM2(0);
            lkAn2.Visible = true;
            lkXem2.Visible = false;
            lbXem.Visible = false;
            lkAn.Visible = false;
        }

        protected void btnAn2_Click(object sender, EventArgs e)
        {
            loadDM2(10);
            lkXem2.Visible = true;
            lkAn2.Visible = false;
        }
    }
}